package com.yanqu.road.server.manger.config.grandchild.talent.utils;

import com.yanqu.road.entity.config.grandchild.GrandChildTalentInfo;
import com.yanqu.road.entity.config.skill.SkillInfo;
import com.yanqu.road.entity.grandchild.GrandChildData;
import com.yanqu.road.server.manger.config.SkillMgr;
import com.yanqu.road.server.manger.config.grandchild.GrandChildConfigMgr;

import com.yanqu.road.server.manger.config.grandchild.talent.entity.GrandChildTalentConditionParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerParam;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerParamBuilder;
import com.yanqu.road.server.manger.config.grandchild.talent.entity.TalentTriggerResult;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffect;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillEffectStage;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildSkillFlag;
import com.yanqu.road.server.manger.config.grandchild.talent.enums.eGrandChildTalentTrigger;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TalentUtils {

    /**
     * 获取触发技能结果
     */
    public static List<TalentTriggerResult> getTriggerTalentResult(GrandChildData data, eGrandChildSkillEffectStage stage) {
        List<TalentTriggerResult> list = new ArrayList<>();

        List<GrandChildTalentInfo> orderedTalentInfo = getOrderedTalentInfo(data.getTalentList());
        for (GrandChildTalentInfo talentInfo : orderedTalentInfo) {

            TalentTriggerParam triggerParam = TalentTriggerParamBuilder.aTalentTriggerParam()
                    .setGrandChild(data)
                    .setTalentInfo(talentInfo)
                    .setCondition(getConditionParam(talentInfo.getTriggerCondition()))
                    .setStage(stage)
                    .setLastTriggerList(list)
                    .build();

            TalentTriggerResult result = eGrandChildTalentTrigger.getResult(triggerParam);
            list.add(result);
        }

        return list;
    }

    private static GrandChildTalentConditionParam getConditionParam(String condition) {

        // 是否无条件
        if (Objects.equals(condition, "0")) {
            return new GrandChildTalentConditionParam("NOCOND", eGrandChildSkillFlag.NO_EQ, 0);
        }

        char[] chars = condition.toCharArray();

        // 【1. 找出前几个字母】
        StringBuilder sb1 = new StringBuilder();
        for (int i = 0; i < chars.length; i++) {
            char ch = chars[i];
            if (!(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')) {
                break;
            }
            sb1.append(ch);
        }
        String name = sb1.toString();

        // 【2. 找出中间几个运算符号】
        StringBuilder sb2 = new StringBuilder();
        for (int i = sb1.length(); i < chars.length; i++) {
            char ch = chars[i];
            if (ch >= '0' && ch <= '9') {
                break;
            }
            sb2.append(ch);
        }
        eGrandChildSkillFlag flag = eGrandChildSkillFlag.getByName(sb2.toString());

        // 【3. 最后是数字值】
        StringBuilder sb3 = new StringBuilder();
        for (int i = sb1.length() + sb2.length(); i < chars.length; i++) {
            sb3.append(chars[i]);
        }
        int value = Integer.valueOf(sb3.toString());

        // 【返回】
        return new GrandChildTalentConditionParam(name, flag, value);
    }

    private static List<SkillInfo> getSkillInfo(List<Integer> skillId) {
        List<SkillInfo> result = new ArrayList<>();
        for (int skill : skillId) {
            SkillInfo skillInfo = SkillMgr.getSkillInfo(skill);
            result.add(skillInfo);
        }
        return result;
    }

    public static List<SkillInfo> getSkillInfo(List<Integer> skillId, eGrandChildSkillEffectStage stage) {
        List<SkillInfo> infoList = getSkillInfo(skillId);
        List<SkillInfo> list = new ArrayList<>();
        for (SkillInfo skillInfo : infoList) {
            eGrandChildSkillEffect skillEffect = eGrandChildSkillEffect.getByType(skillInfo.getType());
            if (skillEffect != null && skillEffect.getStage() == stage) {
                list.add(skillInfo);
            }
        }
        return list;
    }

    public static List<Integer> calcFlairList(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        return TalentResultUtils.getFlairList(data, triggerResultList);
    }

    public static int calcQua(GrandChildData data, List<TalentTriggerResult> triggerResultList) {
        return TalentResultUtils.getQua(data, triggerResultList);
    }

    public static List<GrandChildTalentInfo> getOrderedTalentInfo(List<Integer> talentList) {
        List<GrandChildTalentInfo> list = new ArrayList<>();
        for (int talentId : talentList) {
            GrandChildTalentInfo talentInfo = GrandChildConfigMgr.getGrandChildTalentInfo(talentId);
            list.add(talentInfo);
        }
        return list.stream().sorted(Comparator.comparing(GrandChildTalentInfo::getTriggerOrder).reversed()).collect(Collectors.toList());
    }

}
